#include <stdio.h>
#include <stdlib.h>
#include "list.h"  //双向循环链表

//自定义的类型
struct student {
    struct list_head list; //节点类型
    int sno;
};


int main(void)
{
    int i;
    struct student *p;
    struct student head; //头节点
    struct list_head *pos; //保存遍历链表节点的指针
    //初始化内核链表
    INIT_LIST_HEAD(&head.list);

    /**
     * 这里是头插入
     * head<->40<->30<->20<->10<->0<->head
     */
    for (i = 0; i < 5; i++) {
        p = (struct student *)malloc(sizeof(struct student));
        p->sno = i *10;
        list_add(&p->list, &head.list); //头节点后面插入
    }
    printf("head");

    /**
     * 遍历链表，pos 是 list_head list;  
     *  由于
     * struct student {
    struct list_head list; //节点类型 
    int sno;
      }   第一个成员是 list_head 所以student头地址和list重合了，这里可以强转
  //如果是
  struct student {
    int sno;
    struct list_head list; //节点类型
  };
   那么不行，看 2_add.c

       
     */
    list_for_each(pos, &head.list) {        
        printf("<->%d", ((struct student *)pos)->sno);

    }
    printf("<->head\n");
}




